package org.gruposp2p.dnie.client.ui.panel;

import java.util.ArrayList;
import java.util.List;
import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author jj
 */
public class PrivateCentralPanel extends Composite implements ValueChangeHandler<String> {
	
    public static String HISTORY_UPLOADED_DOCUMENTS = "#uploadedDocuments";
    public static String HISTORY_SIGNED_DOCUMENTS = "#signedDocuments";
    
    private static PrivateCentralPanelUiBinder uiBinder = GWT
                    .create(PrivateCentralPanelUiBinder.class);
    
    interface PrivateCentralPanelUiBinder extends UiBinder<Widget, PrivateCentralPanel> { }
    
	interface Style extends CssResource {
		String selected();
    	String notSelected();
	}

	@UiField Style style;

    @UiField VerticalPanel selectedPanel;
    @UiField UploadedDocumentsPanel uploadedDocuments;
    @UiField SignedDocumentsPanel signedDocuments; 
	
    @UiField Hyperlink uploadedDocumentsLink;
	@UiField Hyperlink signedDocumentsLink;

	List<Hyperlink> hyperlinks = null;

    public PrivateCentralPanel() {
        initWidget(uiBinder.createAndBindUi(this));
		hyperlinks = new ArrayList<Hyperlink>();
		hyperlinks.add(uploadedDocumentsLink);
		hyperlinks.add(signedDocumentsLink);
		History.addValueChangeHandler(this);
    }

   public void setPanel (String historyToken) {
	   if (selectedPanel != null) selectedPanel.clear();
	   selectedPanel.setHeight("400px");
       if (historyToken.equals(HISTORY_UPLOADED_DOCUMENTS)){
    	   selectedPanel.add(uploadedDocuments.getInstance());
       } else if (historyToken.equals(HISTORY_SIGNED_DOCUMENTS)){
    	   selectedPanel.add(signedDocuments.getInstance());
       }
    }

	public void setSelected (String selectedMenu) {
		Hyperlink selectedHyperlink = null;
		if (HISTORY_UPLOADED_DOCUMENTS.equals(selectedMenu)) {
			selectedHyperlink = uploadedDocumentsLink;
		} else if (HISTORY_SIGNED_DOCUMENTS.equals(selectedMenu)) {
			selectedHyperlink = signedDocumentsLink;
		}
		if (selectedHyperlink != null) {
			for (Hyperlink hyperlink : hyperlinks) {
				if (hyperlink.equals(selectedHyperlink)) {
					hyperlink.removeStyleName(style.notSelected());
					hyperlink.addStyleName(style.selected());
				} else {
					hyperlink.removeStyleName(style.selected());
					hyperlink.addStyleName(style.notSelected());
				}
			}
			setPanel(selectedMenu);
			MainPanel.INSTANCE.setSelectedPanel(this);
		}
	}

	@Override
	public void onValueChange(ValueChangeEvent<String> event) {
		if (MainPanel.INSTANCE.loading) return; 
		String historyToken = event.getValue();
		if (this.isVisible()) setSelected(historyToken);
	}
    
}